import { reqAlbumList, reqArtistAlbumList,reqArtistFans } from "@/api";
const state = {
  albumLists: {},
  // 歌手其他专辑
  hotAlbums:[],
  // 
  fansList:[]
};
const mutations = {
  GETALBUMLIST(state, data) {
    state.albumLists = data;
  },
  GETRELATEDPLAYLIST(state, data) {
    state.hotAlbums = data || []
  },
  GETARTISTFANS(state,data){
    state.fansList = data || []
  }
};

const actions = {
  // reqAlbumList获取专辑内容
  async getAlbumList({ commit }, id) {
    const result = await reqAlbumList(id);
    // console.log(result);
    if (result.code == 200) {
      // let n = /\n/;
      // this.description = result.album.description.split(n);
      // this.recomposeSongs(result.songs);
      commit("GETALBUMLIST", result || {});
    } else {
      alert("获取专辑内容失败");
    }
  },

  // 获取右边的 歌手其他专辑
   getRelatedPlayList({ state, commit }) {
    setTimeout(async()=>{
      if (state.albumLists.album) {
        const id = state.albumLists.album.artists[0].id;
        // console.log(id)
        // 发送请求
        // 接口地址 : /artist/album
        // 调用例子 : /artist/album?id=6452&limit=5 ( 周杰伦 )
        const prams = {
          id,
          limit: 5,
        };
        const result = await reqArtistAlbumList(prams);
        //  console.log(result)
        if (result.code == 200) {
          commit("GETRELATEDPLAYLIST", result.hotAlbums);
        }else{
          alert('获取歌手其他专辑失败')
        }
      }
    },1000)
  },
  // 获取右边的 歌手粉丝 reqArtistFans
  getArtistFans({state,commit}){
    setTimeout(async()=>{
      if (state.albumLists.album) {
        const id = state.albumLists.album.artists[0].id;
        // console.log(id)
        // 发送请求
        // 接口地址 : /artist/album
        // 调用例子 : /artist/album?id=6452&limit=5 ( 周杰伦 )
        const prams = {
          id,
          limit: 8,
        };
        const result = await reqArtistFans(prams);
         console.log(result)
        if (result.code == 200) {
          commit("GETARTISTFANS", result.data);
        }else{
          alert('获取歌手其他专辑失败')
        }
      }
    },1000)
  }
};

const getters = {
  description(state) {
    if (state.albumLists.album) {
      const len = state.albumLists.album.description;
      let n = /\n/;
      if (len) {
        return len.split(n) || [];
      } else {
        return [];
      }
    }
  },
};

export default {
  state,
  mutations,
  actions,
  getters,
};
